package com.fc.test.api;

import com.fc.test.model.auto.TAbbBiammin;
import com.fc.test.model.auto.TAbbBmliuyan;
import com.fc.test.service.TAbbBiamminService;
import com.fc.test.util.JsonUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "apiBianminController")
@Api(value = "便民信息")
public class ApiBianminController {

    @Autowired
    private TAbbBiamminService biamminService;

    @ResponseBody
    @GetMapping(value = "getBianminByPage",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询便民信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value = "页码",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="type",value = "1，便民区 2，红包区",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "category", value = "分类(红包区传空)", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "用户id", dataType = "String",paramType = "query")
    })
    public String getBianminByPage(Integer page,Integer type,String category,String memberId){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbBiammin> list = biamminService.getBianminByPage(page,type,category,memberId);
        resMap.put("list",list);
        resMap.put("success",true);
        if(list.size()>0){
            resMap.put("msg","获取便民信息成功");
        }else{
            resMap.put("msg","已经到底了");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "releaseBianmin",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "发布便民信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userid",value = "发布人id",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="title",value = "标题",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="category",value = "分类名称",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "price", value = "红包金额", dataType = "Double",paramType = "query"),
            @ApiImplicitParam(name = "lianximember", value = "联系人", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "lianxiphone", value = "联系电话", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "lianxiaddress", value = "联系地址", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "introduction", value = "信息内容", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "images", value = "图片", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "days", value = "置顶天数", dataType = "Integer",paramType = "query")
    })
    public String releaseBianmin(String userid,String title,String category,Double price,String lianximember,String lianxiphone,String lianxiaddress,String introduction,String images,Integer days){
        TAbbBiammin tAbbBiammin = new TAbbBiammin();
        tAbbBiammin.setUserid(userid);
        tAbbBiammin.setTitle(title);
        tAbbBiammin.setCategory(category);
        tAbbBiammin.setPrice(price);
        tAbbBiammin.setLianximember(lianximember);
        tAbbBiammin.setLianxiphone(lianxiphone);
        tAbbBiammin.setLianxiaddress(lianxiaddress);
        tAbbBiammin.setIntroduction(introduction);
        tAbbBiammin.setImages(images);
        tAbbBiammin.setDays(days);

        Map<String,Object> resMap = biamminService.releaseBianmin(tAbbBiammin);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "getBianinDetail",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "便民信息详情")
    public String getBianinDetail(
            @ApiParam(value = "便民信息id") @RequestParam(value = "id") String id,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = biamminService.getBianinDetail(id,userid);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value = "getBianminRedPcket",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取便民红包")
    public String getBianminRedPcket(
            @ApiParam(value = "便民信息id") @RequestParam(value = "id") String id,
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = biamminService.getBianminRedPcket(id,userid);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="addPinglun",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "发布便民信息留言")
    public String addPinglun(
            @ApiParam(value = "便民信息id") @RequestParam(value = "bmid") String bmid,
            @ApiParam(value = "留言者id") @RequestParam(value = "userid") String userid,
            @ApiParam(value = "留言内容") @RequestParam(value = "content") String content
    ){
        Map<String,Object> resMap = new HashMap<>();
        int flag = biamminService.addPinglun(bmid,userid,content);
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","发布成功");
        }else{
            resMap.put("success",true);
            resMap.put("msg","发布成功");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="addHuifu",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "回复便民信息留言")
    public String addHuifu(
            @ApiParam(value = "留言id") @RequestParam(value = "lyid") String lyid,
            @ApiParam(value = "留言者id") @RequestParam(value = "userid") String userid,
            @ApiParam(value = "回复内容") @RequestParam(value = "content") String content
    ){
        Map<String,Object> resMap = new HashMap<>();
        int flag = biamminService.addHuifu(lyid,userid,content);
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","发布成功");
        }else{
            resMap.put("success",true);
            resMap.put("msg","发布成功");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value="getLiuyanList",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取便民信息详情页留言列表")
    public String getLiuyanList(
            @ApiParam(value = "便民信息id") @RequestParam(value = "bmid") String bmid,
            @ApiParam(value = "页码") @RequestParam(value = "page") int  page
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<Map<String,Object>> list = biamminService.getLiuyanList(bmid,page);
        if(list.size()>0){
            resMap.put("msg","获取评论列表成功");
        }else{
            resMap.put("msg","到底了");
        }
        resMap.put("success",true);
        resMap.put("list",list);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @GetMapping(value = "getMyBianminList",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询我的便民信息")
    public String getMyBianminList(
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid,
            @ApiParam(value = "页码") @RequestParam(value = "page") int  page
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbBiammin> list = biamminService.getMyBianminList(page,userid);
        resMap.put("list",list);
        resMap.put("success",true);
        if(list.size()>0){
            resMap.put("msg","获取我发布的便民信息成功");
        }else{
            resMap.put("msg","已经到底了");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value = "deleteBianmin",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "分页查询我的便民信息")
    public String deleteBianmin(
            @ApiParam(value = "便民信息id") @RequestParam(value = "bmid") String bmid
    ){
        Map<String,Object> resMap = biamminService.deleteBianmin(bmid);
        return JsonUtil.jsonMapStr(resMap);
    }
}
